import {createRouter, createWebHashHistory} from 'vue-router'
import {getRouter} from './routerUtil'
let routes = [
    {
        path: '/',
        name: 'login',
        component: () => import('@/sys/base/login/login')
    },
    {
        path: '/home',
        name: 'home',
        component: () => import('@/sys/base/home'),
        children:[]
    }

]


function getToken(){
    return window.localStorage.getItem("token");
}
let routerDataStr = window.localStorage.getItem("router")
routes[1].children = getRouter(JSON.parse(routerDataStr))

const router = createRouter({
    history: createWebHashHistory(),
    routes
});
router.beforeEach((to, from, next)=>{

    if(to.name === "login"){
        next()
    }
    if(!getToken()){
        next({name: 'login'})

    }
    next()
})
router.onError(() => {
    router.push({name:'404'})
});
router.afterEach(() => {

});

export default router
